package org.chs.ts.batch.threadservice;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.chs.ts.base.batch.exec.entity.BatchClearBasicTaskExec;
import org.chs.ts.base.batch.exec.entity.BatchClearBasicTaskStepExec;
import org.chs.ts.base.batch.exec.entity.BatchDailyBasicTaskStepExec;
import org.chs.ts.base.batch.log.entity.BatchClearBasicLog;
import org.chs.ts.base.batch.set.entity.BatchClearBasicGroupInfoSet;
import org.chs.ts.base.batch.vo.entity.BatchDailyExecReq;
import org.chs.ts.base.batch.vo.entity.BatchDailyExecResp;
import org.chs.ts.base.batch.vo.entity.BatchExecReq;
import org.chs.ts.base.batch.vo.entity.BatchExecResp;
import org.chs.ts.base.common.util.oConvertUtils;
import org.chs.ts.batch.constant.TaskDailyStatusCodeEnum;
import org.chs.ts.batch.constant.TaskStatusCodeEnum;
import org.chs.ts.batch.util.CommonUtil;
import org.chs.ts.batch.exception.TaskDailyException;
import org.chs.ts.batch.exception.TaskExecException;

import org.chs.ts.batch.service.IBatchClearBasicGroupInfoSetService;
import org.chs.ts.batch.service.IBatchClearBasicLogService;
import org.chs.ts.batch.service.IBatchClearBasicTaskStepExecService;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Compay 盛唐天行
 * @Author: wuwei
 * @Censor By wuwei
 * @Sence 2023/1/9 9:13
 * @Modify 2023/1/9 9:13
 * @Description
 */
@Component
@Scope("prototype")
@Slf4j
public class BatchExecService {

    @Resource
    private ApplicationContext ac;

    @Resource
    private Environment environment;



    @Resource
    private IBatchClearBasicGroupInfoSetService clearBasicGroupInfoSetService;

    @Resource
    private IBatchClearBasicTaskStepExecService clearBasicTaskStepExecService;



    @Resource
    private IBatchClearBasicLogService clearBasicLogService;

    @Async("threadPoolBatchTaskExecutor")
    public void asyncDoProcess(BatchExecReq batchExecReq, BatchClearBasicTaskExec taskExec, List<BatchClearBasicTaskStepExec> taskStepExecList) {
        try {

            execute(batchExecReq, taskExec, taskStepExecList);
        } catch (TaskExecException e) {
            e.printStackTrace();
            log.error("异步调用清算任务失败，失败原因：");
            log.error(e.getMessage(), e);
            try {
                batchErrorSendMessage(taskExec);
            } catch (Exception ex) {
                log.error("发送短信异常失败原因："+ex.getMessage());

            }
        }

    }

    @Async("threadPoolBatchTaskExecutor")
    public void asyncDoProcess(BatchDailyExecReq request, BatchDailyBasicTaskStepExec taskDailyExec) {
        try {

            execute(request, taskDailyExec);
        } catch (TaskDailyException e) {
            e.printStackTrace();
            log.error("异步调用清算任务失败，失败原因：");
            log.error(e.getMessage(), e);
            try {
                batchErrorSendMessage(taskDailyExec);
            } catch (Exception ex) {
                log.error("发送短信异常失败原因："+ex.getMessage());

            }
        }

    }

    private void batchErrorSendMessage(BatchDailyBasicTaskStepExec taskDailyExec) {
        int MAX_COUNT = 3;
        int i = 0;
        boolean result = false;

        String execDate = "跑批日期" + taskDailyExec.getExecDate();
        //todo-wuwei:待定
        String errMsg ="";
//
//        String errMsg = taskGroupObj.getTaskGroupName() + (taskStepExecList.size() == 0 ? "" : "," + taskStepExecList.get(0).getStepName()) + "运行异常";
//        SMS001Request sms001Request = new SMS001Request();
//        sms001Request.setTmg(writeSms001RequestTmg(taskGroupObj));
//        // 业务参数: 键值对
//        JSONObject obj = new JSONObject();
//        obj.put("execDate", execDate);
//        obj.put("errMsg", errMsg);
//        sms001Request.setBsnParm(JSONObject.toJSONString(obj));
//        do {
//            i++;
//            SMS002Response sms002Response;
//            try {
//                UserTokenContext.setToken(tempTokenUtil.getTempToken());
//
//                //taskExec.getTaskGroup()
//                sms002Response = iTransApi.batchErrorSendMessage(sms001Request);
//
//            } catch (Exception ex) {
//                ex.printStackTrace();
//                continue;
//            } finally {
//                UserTokenContext.remove();
//            }
//
//            result = sms002Response.isResults();
//            if (result) {
//                break;
//            }
//            try {
//                Thread.sleep(i * 5000L);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//
//        } while (i < MAX_COUNT);

        // 记录日志

        BatchClearBasicLog clearBasicLog = clearBasicLogService.list(new LambdaQueryWrapper<BatchClearBasicLog>().eq(BatchClearBasicLog::getTaskExecid, taskDailyExec.getTaskExecid()).orderByDesc(BatchClearBasicLog::getId)).get(0);
        clearBasicLog.setSysOrgCode("短信发送" + (result ? "成功" : "失败"));
        log.info("batchErrorSendMessage -> 短信发送" + (result ? "成功" : "失败") + " 短信内容:" + errMsg);
        clearBasicLogService.updateById(clearBasicLog);

    }




    public void batchErrorSendMessage(BatchClearBasicTaskExec taskExec) {
        int MAX_COUNT = 3;
        int i = 0;
        boolean result = false;
        // 获取跑批组对象
        BatchClearBasicGroupInfoSet taskGroupObj = clearBasicGroupInfoSetService.getOne(new LambdaQueryWrapper<BatchClearBasicGroupInfoSet>().eq(BatchClearBasicGroupInfoSet::getBatchSysCode, taskExec.getBatchSysCode()).eq(BatchClearBasicGroupInfoSet::getTaskGroup, taskExec.getTaskGroup()));
        // 获取跑批子对象
        List<BatchClearBasicTaskStepExec> taskStepExecList = clearBasicTaskStepExecService.list(new LambdaQueryWrapper<BatchClearBasicTaskStepExec>()
                .eq(BatchClearBasicTaskStepExec::getBatchSysCode, taskExec.getBatchSysCode())
                .eq(BatchClearBasicTaskStepExec::getTaskGroup, taskExec.getTaskGroup())
                .eq(BatchClearBasicTaskStepExec::getTaskId, taskExec.getTaskId())
                .eq(BatchClearBasicTaskStepExec::getExecDate, taskExec.getExecDate())
                .in(BatchClearBasicTaskStepExec::getExecStatus, new ArrayList<>(Collections.singletonList("4"))));
        String execDate = "跑批日期" + taskExec.getExecDate();
        //todo-wuwei:待定
        String errMsg ="";
//
//        String errMsg = taskGroupObj.getTaskGroupName() + (taskStepExecList.size() == 0 ? "" : "," + taskStepExecList.get(0).getStepName()) + "运行异常";
//        SMS001Request sms001Request = new SMS001Request();
//        sms001Request.setTmg(writeSms001RequestTmg(taskGroupObj));
//        // 业务参数: 键值对
//        JSONObject obj = new JSONObject();
//        obj.put("execDate", execDate);
//        obj.put("errMsg", errMsg);
//        sms001Request.setBsnParm(JSONObject.toJSONString(obj));
//        do {
//            i++;
//            SMS002Response sms002Response;
//            try {
//                UserTokenContext.setToken(tempTokenUtil.getTempToken());
//
//                //taskExec.getTaskGroup()
//                sms002Response = iTransApi.batchErrorSendMessage(sms001Request);
//
//            } catch (Exception ex) {
//                ex.printStackTrace();
//                continue;
//            } finally {
//                UserTokenContext.remove();
//            }
//
//            result = sms002Response.isResults();
//            if (result) {
//                break;
//            }
//            try {
//                Thread.sleep(i * 5000L);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//
//        } while (i < MAX_COUNT);

        // 记录日志

        BatchClearBasicLog clearBasicLog = clearBasicLogService.list(new LambdaQueryWrapper<BatchClearBasicLog>().eq(BatchClearBasicLog::getTaskExecid, taskExec.getTaskExecid()).orderByDesc(BatchClearBasicLog::getId)).get(0);
        clearBasicLog.setSysOrgCode("短信发送" + (result ? "成功" : "失败"));
        log.info("batchErrorSendMessage -> 短信发送" + (result ? "成功" : "失败") + " 短信内容:" + errMsg);
        clearBasicLogService.updateById(clearBasicLog);

    }

    /**
     * 返回 短信发送指定时间
     *
     * @param taskGroupSetObj 跑批组对象
     * @return 短信指定发送时间
     */
    private String writeSms001RequestTmg(BatchClearBasicGroupInfoSet taskGroupSetObj) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        // 默认
        String tmg = "";
        String errMsgSendType = taskGroupSetObj.getErrMsgSendType();
        // 2-自然日工作时间发送(8:00-18:00)
        if ("2".equals(errMsgSendType)) {
            // 初始化 当前时间8:00-18:00 短信延迟一天的上午 8:00 发送
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            // 当前时间 非
            if (hour > 18 || hour < 8) {
                calendar.add(Calendar.DATE, 1);
                tmg = new SimpleDateFormat("yyyy/MM/dd").format(calendar.getTime()) + " 08:00:00";
            }
        }
        if ("3".equals(errMsgSendType)) {
            // 3-工作日工作时间发送(8:00-18:00)
            // 获取 当前星期
            int week = calendar.getFirstDayOfWeek() == Calendar.SUNDAY ? ((calendar.get(Calendar.DAY_OF_WEEK) - 1) == 0 ? 7 : (calendar.get(Calendar.DAY_OF_WEEK) - 1)) : calendar.get(Calendar.DAY_OF_WEEK);
            // 当前 星期 非 星期一至星期五 短信延迟到后续星期一的上午 8:00 发送
            if (week == 6 || week == 7) {
                calendar.add(Calendar.DATE, 8 - week);
                tmg = new SimpleDateFormat("yyyy/MM/dd").format(calendar.getTime()) + " 08:00:00";
            } else {
                //当前时间8:00-18:00 短信延迟一天的上午 8:00 发送(如果当前为星期五 将延迟三天发送)
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                if (hour > 18 || hour < 8) {
                    calendar.add(Calendar.DATE, week == 5 ? 3 : 1);
                    tmg = new SimpleDateFormat("yyyy/MM/dd").format(calendar.getTime()) + " 08:00:00";
                }
            }

        }
        return tmg;

    }

    @Async("threadPoolBatchTaskExecutor")
    public void asyncDoTradeCapitalCheckError(String errorSerno) {

//        //todo-wuwei:待定
//        MwbpTradeCapitalCheckError tradeCapitalCheckError = tradeCapitalCheckErrorService.getOne(new LambdaQueryWrapper<MwbpTradeCapitalCheckError>().eq(MwbpTradeCapitalCheckError::getErrorSerno, errorSerno));
//
//        // 初始化差错处理日志
//        String checkErrorLogSerno = createCheckErrorLog(tradeCapitalCheckError);
//
//        String errOptType = tradeCapitalCheckError.getErrOptType();
//        Map<String, String> responseRetCdAndMsg;
//        int query_i = 0;
//        int correct_i = 0;
//
//        // 线程等待时间
//        long THREAD_WAITTING_TIME = 5000;
//        // 最大查询次数
//        int MAX_QUERY_COUNT = 3;
//        if (errOptType.equals(EnumUtil.资金交易对账差错表_差错处理类型_冲正)) {
//
//            // 冲正操作
//            // 最大冲正次数
//            int MAX_CORRECT_COUNT = 3;
//            while (correct_i < MAX_CORRECT_COUNT) {
//                // 操作前 优先查询 acctResult: S成功, R冲正
//                responseRetCdAndMsg = tradeCapitalBuilder.getTradeCapitalOpt(tradeCapitalCheckError, "query");
//                // 查询成功 且 已冲正
//                if (RetCode.SUCCESS.equals(responseRetCdAndMsg.get("retCd")) && "R".equals(responseRetCdAndMsg.get("acctResult"))) {
//                    // 更新资金流水状态 结束当前调账操作
//                    //updateTradeCapitalTransLogFormCapitalDealStatusAndChkStatus(tradeCapitalCheckError, EnumUtil.客户资金交易流水表_对账状态_对账相符, EnumUtil.客户资金交易流水表_资金流水状态_冲正成功);
//                    updateCheckErrorLog(checkErrorLogSerno, responseRetCdAndMsg.get("retCd"), responseRetCdAndMsg.get("msg"), EnumUtil.客户资金交易流水表_资金流水状态_冲正成功);
//                    updateTradeCapitalTransLogFormCapitalDealStatusAndChkStatus(tradeCapitalCheckError, EnumUtil.客户资金交易流水表_资金流水状态_冲正成功);
//                    return;
//                }
//
//                // 发起冲正操作
//                responseRetCdAndMsg = tradeCapitalBuilder.getTradeCapitalOpt(tradeCapitalCheckError, "opt");
//
//                // 冲正成功 明确反馈 冲正成功 无需对账 直接更新资金流水状态
//                if (RetCode.SUCCESS.equals(responseRetCdAndMsg.get("retCd"))) {
//
//                    // 更新 资金流水对账状态
//                    //updateTradeCapitalTransLogFormCapitalDealStatusAndChkStatus(tradeCapitalCheckError, EnumUtil.客户资金交易流水表_对账状态_对账相符, EnumUtil.客户资金交易流水表_资金流水状态_冲正成功);
//                    updateCheckErrorLog(checkErrorLogSerno, responseRetCdAndMsg.get("retCd"), responseRetCdAndMsg.get("msg"), EnumUtil.客户资金交易流水表_资金流水状态_冲正成功);
//
//                    updateTradeCapitalTransLogFormCapitalDealStatusAndChkStatus(tradeCapitalCheckError, EnumUtil.客户资金交易流水表_资金流水状态_冲正成功);
//
//                    return;
//                }
//                // 冲正超时 查询获取记账结果
//                if (RetCode.ERR_TIME_OUT.equals(responseRetCdAndMsg.get("retCd"))) {
//                    while (query_i < MAX_QUERY_COUNT) {
//                        responseRetCdAndMsg = tradeCapitalBuilder.getTradeCapitalOpt(tradeCapitalCheckError, "query");
//                        // 查询成功 且 已冲正
//                        if (RetCode.SUCCESS.equals(responseRetCdAndMsg.get("retCd")) && "R".equals(responseRetCdAndMsg.get("acctResult"))) {
//                            // 更新资金流水状态 结束当前调账操作
//                            //updateTradeCapitalTransLogFormCapitalDealStatusAndChkStatus(tradeCapitalCheckError, EnumUtil.客户资金交易流水表_对账状态_对账相符, EnumUtil.客户资金交易流水表_资金流水状态_冲正成功);
//                            updateCheckErrorLog(checkErrorLogSerno, responseRetCdAndMsg.get("retCd"), responseRetCdAndMsg.get("msg"), EnumUtil.客户资金交易流水表_资金流水状态_冲正成功);
//                            updateTradeCapitalTransLogFormCapitalDealStatusAndChkStatus(tradeCapitalCheckError, EnumUtil.客户资金交易流水表_资金流水状态_冲正成功);
//                            return;
//                        } else {
//                            // 查询超时 或 查询失败 当前线程等待 5秒
//                            try {
//                                Thread.sleep(THREAD_WAITTING_TIME);
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                        }
//
//                        query_i++;
//
//                    }
//
//                }
//                correct_i++;
//            }
//
//        }
//
//        if (errOptType.equals(EnumUtil.资金交易对账差错表_差错处理类型_补记账)) {
//            // 操作前 优先查询 acctResult: S成功, R冲正
//            //todo-wuwei:待定
//            //responseRetCdAndMsg = tradeCapitalBuilder.getTradeCapitalOpt(tradeCapitalCheckError, "query");
//            // TODO=wuwei:针对 查询结果 返回内容进行操作
//            // 查询成功 且 已成功
//            if (RetCode.SUCCESS.equals(responseRetCdAndMsg.get("retCd")) && "S".equals(responseRetCdAndMsg.get("acctResult"))) {
//                // 更新资金流水状态 结束当前调账操作
//                //updateTradeCapitalTransLogFormCapitalDealStatusAndChkStatus(tradeCapitalCheckError, EnumUtil.客户资金交易流水表_对账状态_对账相符, EnumUtil.客户资金交易流水表_资金流水状态_记账成功);
//                updateCheckErrorLog(checkErrorLogSerno, responseRetCdAndMsg.get("retCd"), responseRetCdAndMsg.get("msg"), EnumUtil.客户资金交易流水表_资金流水状态_记账成功);
//                //todo-wuwei:待定
//                //updateTradeCapitalTransLogFormCapitalDealStatusAndChkStatus(tradeCapitalCheckError, EnumUtil.客户资金交易流水表_资金流水状态_记账成功);
//                return;
//            }
//
//            //todo-wuwei:待定
//            // 发起补记账
//            //responseRetCdAndMsg = tradeCapitalBuilder.getTradeCapitalOpt(tradeCapitalCheckError, "");
//
//            // 补记账 明确反馈 补记账成功 无需对账 直接更新资金流水状态
//            if (RetCode.SUCCESS.equals(responseRetCdAndMsg.get("retCd"))) {
//
//                // 更新 资金流水对账状态
//                //updateTradeCapitalTransLogFormCapitalDealStatusAndChkStatus(tradeCapitalCheckError, EnumUtil.客户资金交易流水表_对账状态_对账相符, EnumUtil.客户资金交易流水表_资金流水状态_记账成功);
//                updateCheckErrorLog(checkErrorLogSerno, responseRetCdAndMsg.get("retCd"), responseRetCdAndMsg.get("msg"), EnumUtil.客户资金交易流水表_资金流水状态_记账成功);
//                //todo-wuwei:待定
//                //updateTradeCapitalTransLogFormCapitalDealStatusAndChkStatus(tradeCapitalCheckError, EnumUtil.客户资金交易流水表_资金流水状态_记账成功);
//
//                return;
//            }
//
//            // 补记账超时 查询获取记账结果
//            if (RetCode.ERR_TIME_OUT.equals(responseRetCdAndMsg.get("retCd"))) {
//                while (query_i < MAX_QUERY_COUNT) {
//                    //todo-wuwei:待定
//                    //responseRetCdAndMsg = tradeCapitalBuilder.getTradeCapitalOpt(tradeCapitalCheckError, "query");
//                    // 查询成功 且 成功
//                    if (RetCode.SUCCESS.equals(responseRetCdAndMsg.get("retCd")) && "S".equals(responseRetCdAndMsg.get("acctResult"))) {
//                        // 更新资金流水状态 结束当前调账操作
//                        // updateTradeCapitalTransLogFormCapitalDealStatusAndChkStatus(tradeCapitalCheckError, EnumUtil.客户资金交易流水表_对账状态_对账相符, EnumUtil.客户资金交易流水表_资金流水状态_记账成功);
//                        updateCheckErrorLog(checkErrorLogSerno, responseRetCdAndMsg.get("retCd"), responseRetCdAndMsg.get("msg"), EnumUtil.客户资金交易流水表_资金流水状态_记账成功);
//                        //todo-wuwei:待定
//                        //updateTradeCapitalTransLogFormCapitalDealStatusAndChkStatus(tradeCapitalCheckError, EnumUtil.客户资金交易流水表_资金流水状态_记账成功);
//
//                        return;
//                    } else {
//                        // 查询超时 或 查询失败 当前线程等待 5秒
//                        try {
//                            Thread.sleep(THREAD_WAITTING_TIME);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//
//                    query_i++;
//
//                }
//
//            }
//
//
//        }
//
//        // 针对 超过 最大执行次数 未成功差错 ，回滚差错处理状态 为未处理
//        if (correct_i >= MAX_QUERY_COUNT) {
//            tradeCapitalCheckError.setDealStatus(EnumUtil.资金交易对账差错表_差错状态_未处理);
//            updateCheckErrorLog(checkErrorLogSerno, RetCode.FAILED, "", "");
//            //todo-wuwei:待定
//            //tradeCapitalCheckErrorService.updateById(tradeCapitalCheckError);
//        }

    }

    //private void updateCheckErrorLog(String checkErrorLogSerno, String respCode, String respMsg, String capitalStatus) {
    //    MwbpTradeCapitalCheckErrorLog checkErrorLogObj = tradeCapitalCheckErrorLogService.getOne(new LambdaQueryWrapper<MwbpTradeCapitalCheckErrorLog>().eq(MwbpTradeCapitalCheckErrorLog::getErrorLogSerno, checkErrorLogSerno));
    //    checkErrorLogObj.setErrorResponseSeqNo("中间业务系统-后管人工触发处理");
    //    checkErrorLogObj.setErrorResponseCode(respCode);
    //    checkErrorLogObj.setErrorResponseMsg(respMsg);
    //    checkErrorLogObj.setCapitalStatus(capitalStatus);
    //    tradeCapitalCheckErrorLogService.updateById(checkErrorLogObj);
    //}

    /**
     * 创建 差错处理日志
     *
     * @param tradeCapitalCheckError 差错处理对象
     */
    //private String createCheckErrorLog(MwbpTradeCapitalCheckErrorLog tradeCapitalCheckError) {
    //    // 差错流水 生成规则 差错编号+年月日时分秒
    //    String errorLogSerno = RequestBuilder.errCapitalSerNo();
    //    MwbpTradeCapitalCheckErrorLog checkErrorLogObj = new MwbpTradeCapitalCheckErrorLog();
    //    //checkErrorLogObj.setId("");
    //    //checkErrorLogObj.setCreateBy("");
    //    //checkErrorLogObj.setCreateTime(new Date());
    //    //checkErrorLogObj.setUpdateBy("");
    //    //checkErrorLogObj.setUpdateTime(new Date());
    //    //checkErrorLogObj.setSysOrgCode("");
    //    checkErrorLogObj.setErrorLogSerno(errorLogSerno);
    //    checkErrorLogObj.setErrorSerno(tradeCapitalCheckError.getErrorSerno());
    //    checkErrorLogObj.setCapitalCode(tradeCapitalCheckError.getCapitalCode());
    //    checkErrorLogObj.setTransSeqNo(tradeCapitalCheckError.getTransSeqNo());
    //    //checkErrorLogObj.setRespSeqNo("");
    //    //checkErrorLogObj.setRespCode("");
    //    //checkErrorLogObj.setRespMsg("");
    //    //checkErrorLogObj.setCapitalDealStatus("");
    //    checkErrorLogObj.setChannelType(tradeCapitalCheckError.getChannelType());
    //    checkErrorLogObj.setTransTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    //    checkErrorLogObj.setErrOptType(tradeCapitalCheckError.getErrOptType());
    //
    //    tradeCapitalCheckErrorLogService.save(checkErrorLogObj);
    //
    //
    //    return errorLogSerno;
    //
    //}

    //todo-wuwei:待定

    /**
     * 更新资金流水 capital_deal_status-资金流水状态为7记账失败 chk_status-对账状态为4不需要对账
     */
//    private void updateTradeCapitalTransLogFormCapitalDealStatusAndChkStatus(MwbpTradeCapitalCheckError tradeCapitalCheckError, String capitalDealStatus) {
//        String chkStatus = EnumUtil.客户资金交易流水表_对账状态_对账相符;
//        BatchTradeCapitalTransReq updateRequest = new BatchTradeCapitalTransReq();
//        updateRequest.setChkStatus(chkStatus);
//        updateRequest.setCapitalDealStatus(capitalDealStatus);
//        updateRequest.setCapitalSerno(tradeCapitalCheckError.getCapitalSerno());
//        if (tradeCapitalTransLogService.updateTradeCapitalTransCapitalDealStatusAndChkStatus(updateRequest) == 0) {
//            tradeCapitalCheckError.setDealStatus(EnumUtil.资金交易对账差错表_差错状态_未处理);
//            tradeCapitalCheckErrorService.updateById(tradeCapitalCheckError);
//        } else {
//            tradeCapitalCheckError.setDealStatus(EnumUtil.资金交易对账差错表_差错状态_人工线上处理);
//            tradeCapitalCheckErrorService.updateById(tradeCapitalCheckError);
//        }
//
//
//    }
    public BatchExecResp execute(BatchExecReq batchExecReq, BatchClearBasicTaskExec taskExec, List<BatchClearBasicTaskStepExec> taskStepExecList) throws TaskExecException {
        log.debug("doProcess start ,taskid[" + batchExecReq.getTaskId() + "]");
        if (!StringUtils.hasLength(batchExecReq.getTaskExecId())) {
            throw new TaskExecException("BatchExecService doProcess running error!taskExecId is null or empty!", TaskStatusCodeEnum.TASK_ERROR, batchExecReq.getTaskId());
        }
        BatchServiceBase service = null;
        // 获取执行服务类
        try {


            String port = environment.getProperty("server.port");
            String ip = CommonUtil.getLocalHostAddress();
            //ip = ip.equals("") ? InetAddress.getLocalHost().getHostAddress() : ip;
            String path = oConvertUtils.getString(environment.getProperty("server.servlet.context-path"));

            Class<?> serviceClass = Class.forName(batchExecReq.getSliceServiceClass());
            service = (BatchServiceBase) ac.getBean(serviceClass);
            // 初始化 跑批任务和跑批子任务
            service.initParamter(taskExec, taskStepExecList, ip, port, path);
            BatchExecResp execute = service.execute(batchExecReq);
            // 手动回收bean
            //ac.getAutowireCapableBeanFactory().destroyBean(service);
            return execute;

        } catch (Exception e) {
            // 跑批平台异常 导致 执行器失效 更新 当前任务状态
            // 20240220 注释
//            if (service != null) {
//                BatchExecResp resp = new BatchExecResp();
//                resp.setTaskExecId(batchExecReq.getTaskExecId());
//                resp.setTaskGroup(batchExecReq.getTaskGroup());
//                resp.setTaskId(batchExecReq.getTaskId());
//                resp.setRtnDesc(e.getMessage());
//
//                resp.setRtnCode(RetCode.FAILED);
//                service.writeBackResult(resp, taskExec);
//            }

            throw new TaskExecException("BatchExecService doProcess running error! taskExecExecExption do not operate!" + "[taskExecId:" + batchExecReq.getTaskExecId() + "]" + e.getMessage(), TaskStatusCodeEnum.TASK_ERROR, batchExecReq.getTaskId());
        } //new TaskExecException("BatchExecService -> doProcess exception! " + batchExecReq.getSliceServiceClass() + "ClassNotFoundException", TaskStatusCodeEnum.TASK_ERROR, batchExecReq.getTaskId());


        //return null;
    }

    private BatchDailyExecResp execute(BatchDailyExecReq request, BatchDailyBasicTaskStepExec taskDailyExec) throws TaskDailyException {
        log.debug("doProcess start ,taskExecid[" + request.getTaskExecid() + "]");
        if (!StringUtils.hasLength(request.getTaskExecid())) {
            throw new TaskDailyException("BatchExecService doProcess running error!taskExecid is null or empty!", TaskDailyStatusCodeEnum.TASK_ERROR, request.getTaskExecid());
        }
        BatchDailyServiceBase service = null;
        // 获取执行服务类
        try {
            String port = environment.getProperty("server.port");
            String ip = CommonUtil.getLocalHostAddress();
            //ip = ip.equals("") ? InetAddress.getLocalHost().getHostAddress() : ip;
            String path = oConvertUtils.getString(environment.getProperty("server.servlet.context-path"));

            Class<?> serviceClass = Class.forName(request.getSliceServiceBaseClass());

            service = (BatchDailyServiceBase) ac.getBean(serviceClass);
            // 初始化 跑批任务和跑批子任务
            service.initParamter(request.getTaskPublishObj(), taskDailyExec, ip, port, path);
            BatchDailyExecResp execute = service.execute(request);
            // 手动回收bean
            //ac.getAutowireCapableBeanFactory().destroyBean(service);
            return execute;

        } catch (Exception e) {
            // 跑批平台异常 导致 执行器失效 更新 当前任务状态
            // 20240220 注释
//            if (service != null) {
//                BatchExecResp resp = new BatchExecResp();
//                resp.setTaskExecId(batchExecReq.getTaskExecId());
//                resp.setTaskGroup(batchExecReq.getTaskGroup());
//                resp.setTaskId(batchExecReq.getTaskId());
//                resp.setRtnDesc(e.getMessage());
//
//                resp.setRtnCode(RetCode.FAILED);
//                service.writeBackResult(resp, taskExec);
//            }

            throw new TaskDailyException("BatchExecService doProcess running error! taskDailyExption do not operate!" + "[taskExecId:" + request.getTaskExecid() + "]" + e.getMessage(), TaskDailyStatusCodeEnum.TASK_ERROR, request.getTaskExecid());
        } //new TaskExecException("BatchExecService -> doProcess exception! " + batchExecReq.getSliceServiceClass() + "ClassNotFoundException", TaskStatusCodeEnum.TASK_ERROR, batchExecReq.getTaskId());


        //return null;
    }


}
